from django.shortcuts import render,redirect
#导包
from django.http import HttpResponse,HttpResponseRedirect,JsonResponse
#导入类视图
from django.views import View

from myapp.models import User,Goods,Goods_Exam
import json
from django.core.serializers import serialize
from rest_framework.response import Response
from rest_framework.views import APIView
#导入加密库
import hashlib
#导入图片库
#绘画库
from PIL import ImageDraw
#字体库
from PIL import ImageFont
#图片库
from PIL import Image
#随机库
import random
#文件流
import io

import requests

#导入上传文件夹配置
from mydjango.settings import UPLOAD_ROOT
import os

#导入原生sql模块
from django.db import connection

import jwt

#导入redis数据库
import redis

#导入时间模块
import time

#导入公共目录变量
from mydjango.settings import BASE_DIR

#导包
from django.db.models import Q,F

#导入dwebsocket的库
from dwebsocket.decorators import accept_websocket
import uuid

#导入序列化对象
from myapp.myser import UserSer,GoodsSer,GoodsSer_Exam

from pymongo import  MongoClient

mongocli = MongoClient(host='localhost',port=27017)

#导入bson
from bson import json_util as jsonb

#定义地址和端口
host = '127.0.0.1'
port = 6379

#建立redis连接
r = redis.Redis(host=host,port=port)

#商品列表接口
class GoodsList_Exam(APIView):
    def get(self,request):
        #当前页
        page = int(request.GET.get('page',1))
        #一页有多少条商品
        size = int(request.GET.get('size',1))
        #从哪开始切
        data_start = (page-1) * size
        #定义切到哪
        data_end = page * size
        #查询数据
        goodslist = Goods_Exam.objects.all()[data_start:data_end]
        #查询总数——count返回整形的数量
        count = Goods_Exam.objects.count()
        #序列化
        goods_ser = GoodsSer_Exam(goodslist,many=True)
        res = {
            'total':count,
            'data':goods_ser.data
        }
        return Response(res)


#添加商品标签
class InsertTags_Exam(APIView):
    def get(self,request):
        id = request.GET.get('id',None)
        tags = request.GET.get('tags',None)
        tags = tags.split(",")

        #建立数据库对象
        db = mongocli.mdshop
        #建立表对象
        table = db.tags_exam

        #排重,商品id不能重复
        res = table.find({'gid':str(id)}).count()
        if res >0:
            return Response({"message":'重复数据！'})
        else:
            table.insert({'gid':str(id),'tags':tags})
            return Response({'message':'入库成功'})



#商品入库接口
class AddGoods_Exam(APIView):
    def get(self,request):
        name = request.GET.get('name','null')
        desc = request.GET.get('desc','null')
        parms = request.GET.get('parms','null')
        cate_id = request.GET.get('cate_id','null')
        price = request.GET.get('price','null')

        #排重
        goods = Goods_Exam.objects.filter(name=name).first()

        if goods:
            res = {}
            res['code'] = 405
            res['message'] = '该商品已经存在'
            return Response(res)

        #进行入库
        goods = Goods_Exam(name=name,desc=desc,
                      parms=parms,cate_id=cate_id,price=price)
        goods.save()

        res = {}
        res['code'] = 200
        res['id'] = goods.id
        res['message'] = '商品添加成功'

        return Response(res)